using System;
using Samplum.HostObjects;
using Samplum.UI;
using Samplum.Util;

namespace Samplum.Patches
{
	public class Lp4Algorithm //: MultiPatchElement<Lp4>
	{
		public Lp4Algorithm(Lp4 parent)
		{
			_parent = parent;
		}

		private readonly Lp4 _parent;

		private double t, t2, x, f, k, p, r, y1, y2, y3, y4, oldx, oldy1, oldy2, oldy3;

		private const double _kd = 0;

		public double Process(double value)
		{
			f = (_parent.Frq + _parent.Frq) / Patch.SampleRate;
			p = f * (1.8 - 0.8 * f);
			k = p + p - 1.0;
			t = (1.0 - p) * 1.386249;
			t2 = 12.0 + t * t;
			r = _parent.Res * (t2 + 6.0 * t) / (t2 - 6.0 * t);
			x = value - r * y4;
			y1 = x * p + oldx * p - k * y1;
			y2 = y1 * p + oldy1 * p - k * y2;
			y3 = y2 * p + oldy2 * p - k * y3;
			y4 = y3 * p + oldy3 * p - k * y4;
			y4 = y4 - ((y4 * y4 * y4) / 6.0);
			oldx = x;
			oldy1 = y1 + _kd;
			oldy2 = y2 + _kd;
			oldy3 = y3 + _kd;
			return y4;
		}
	}
}